package com.eunion.common.util;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.eunion.web.admin.entity.TableColumnDic;
import com.eunion.web.admin.entity.spec.TableColumnDicSpec;
import com.eunion.web.admin.repository.TableColumnDicRepository;
import com.eunion.web.cms.entity.University;
import com.eunion.web.cms.entity.spec.UniversitySpec;
import com.eunion.web.cms.repository.UniversityRepository;


@Component
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling
public class BoShiGxmd {

    private static Logger LOGGER = LoggerFactory.getLogger(BoShiGxmd.class);
    @Autowired
    private UniversityRepository repository;

    @Autowired
    private TableColumnDicRepository dicRepository;


    //@Scheduled(cron="0 0 * * * *")
    public void start() {
        List<TableColumnDic> configs = dicRepository.findAll(TableColumnDicSpec.build("config_BoShiGxmd"));
        if (CommUtils.isEmpty(configs)) {
            return;
        }
        String configValue = configs.get(0).getColumnValue();
        if ("1".equals(configValue)) {
            List<TableColumnDic> levelDic = dicRepository.findAll(TableColumnDicSpec.build("level"));
            String charset = "gb2312";
            String url = "http://www.91boshi.net/";

            String urlHtml = HttpClient.doGet(url + "gxmd",charset);
            Document urlDoc = Jsoup.parse(urlHtml);
            Elements urlElements = urlDoc.getElementsByClass("list_b");
            Element areaElemnt = urlElements.get(1);
            Elements aElements = areaElemnt.getElementsByTag("a");

            for (Element aElement : aElements) {
                String href = aElement.attr("href");
                String provinceName = aElement.text();
                String res = HttpClient.doGet(url+href,charset);
                if (StringUtils.isEmpty(res)) {
                    continue;
                }
                Document document = Jsoup.parse(res);
                Elements elements = document.getElementsByTag("tr");
                for (Element element : elements) {
                    Elements tdElements = element.getElementsByTag("td");
                    if (tdElements.size() >= 4) {
                        try {
                            String name = tdElements.get(1).text();
                            String level =  tdElements.get(2).text();
                            String cityName =  tdElements.get(3).text();
                            if(StringUtils.isNotBlank(name)) {
                                University query = new University();
                                query.setName(name);
                                List<University> universities = repository.findAll(UniversitySpec.build(query));
                                if (!CommUtils.isEmpty(universities)) {
                                    for(University university : universities) {
                                        if (StringUtils.isBlank(university.getProvinceName()) || !university.getProvinceName().equals(provinceName)) {
                                            university.setProvinceName(provinceName);
                                        }
                                        if (StringUtils.isBlank(university.getCityName()) || !university.getCityName().equals(cityName)) {
                                            university.setCityName(cityName);
                                        }
                                        if (StringUtils.isNotBlank(level)) {
                                            if (level.contains("本")) {
                                                for(TableColumnDic dic : levelDic) {
                                                    if (dic.getValueMean().contains("本")) {
                                                        university.setLevel(dic.getColumnValue());
                                                    }
                                                }
                                            }
                                            if (level.contains("专")) {
                                                for(TableColumnDic dic : levelDic) {
                                                    if (dic.getValueMean().contains("专")) {
                                                        university.setLevel(dic.getColumnValue());
                                                    }
                                                }
                                            }
                                        }
                                        repository.saveAndFlush(university);
                                    }
                                } else {
                                    //从百度百科获取大学信息
                                    University university = BaikeUtils.getUninversity(name);
                                    if (university != null && 
                                            !StringUtils.isEmpty(university.getCode()) &&
                                            !StringUtils.isEmpty(university.getName()) ) {
                                        if (StringUtils.isBlank(university.getProvinceName()) || !university.getProvinceName().equals(provinceName)) {
                                            university.setProvinceName(provinceName);
                                        }
                                        if (StringUtils.isBlank(university.getCityName()) || !university.getCityName().equals(cityName)) {
                                            university.setCityName(cityName);
                                        }
                                        if (StringUtils.isNotBlank(level)) {
                                            if (level.contains("本")) {
                                                for(TableColumnDic dic : levelDic) {
                                                    if (dic.getValueMean().contains("本")) {
                                                        university.setLevel(dic.getColumnValue());
                                                    }
                                                }
                                            }
                                            if (level.contains("专")) {
                                                for(TableColumnDic dic : levelDic) {
                                                    if (dic.getValueMean().contains("专")) {
                                                        university.setLevel(dic.getColumnValue());
                                                    }
                                                }
                                            }
                                        }
                                        repository.save(university);
                                    }
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
